#!/bin/bash
# se: 安装通道
#
cd $(dirname "$0")
. ./common.sh

require autossh
require screen

INSTALLDIR=$(cd "$(dirname "$0")"; pwd);
QUERY_REMOVE_TUNNEL_SCRIPT=$INSTALLDIR/query-remove-tunnel.sh



help() {
	echo "usage: $0 help"
	echo "       $0 (create|start|stop|status)"
	echo "       $0 start [name]"
	echo "       $0 stop [name]"
	cat <<EOF

help            - this screen
create          - create the tunnel(s)
start           - start the tunnel(s)
start   {name}  - start the tunnel(s)
stop            - stop  the {name} tunnel(s)
stop    {name}  - stop  the {name} tunnel(s)
status          - show the status of the tunnel(s)

EOF
}



if [ "x$1" = "xhelp" ] || [ "x$1" = "x" ]; then
    help
elif [ "x$1" = "xcreate" ]; then

	heading '######################init config######################';
	info 'create local forward tunnel ...'
	info 'Enter tunnel info'
	input_tunnel_name && input_remote_user && input_remote_ip && input_local_port && input_remove_port	
	echo
	if test $exitcode = 0; then
	info 'ssh-keygen start ...'
	ssh_keygen;
	fi
	echo
	info 'ssh-copy start ...'		
	if test $exitcode = 0; then
		ssh_store_file=$ssh_key_comment'_'$TUNNEL_NAME;
		ssh_site=$REMOTE_USER'@'$REMOTE_IP;
		if ! is_confirmed_tunnelsite $ssh_store_file; then
			scp -r $ssh_key_file $ssh_site':/root/.ssh/authorized_keys'
			exitcode=$?;
			result 'ssh-copy success'
			echo
			if test $exitcode = 0; then
				info 'store ssh tunnel start...'
				info 'tunnel configure >> '$ssh_store_file' file'
				echo $TUNNEL_NAME'=L_'$LOCAL_PORT:$REMOTE_IP:$REMOTE_PORT'_'$REMOTE_USER'@'$REMOTE_IP > $confirmed_tmp_dir/$ssh_store_file;
				info 'create '$TUNNEL_NAME' tunnel success'			
			fi
		else
			scp -r $ssh_key_file $ssh_site':/root/.ssh/authorized_keys'
			exitcode=$?;
			result 'ssh-copy success'
			echo
			if test $exitcode = 0; then
			
				rm -rf $ssh_store_file;
				info 'store ssh tunnel start...'
				info 'tunnel configure >> '$ssh_store_file' file'
				echo $TUNNEL_NAME'=L_'$LOCAL_PORT:$REMOTE_IP:$REMOTE_PORT'_'$REMOTE_USER'@'$REMOTE_IP > $confirmed_tmp_dir/$ssh_store_file;
				info 'create '$TUNNEL_NAME' tunnel success'
			fi
		fi			
	else
		help
		exit 1
	fi	
	info 'create local forward tunnel end'

elif [ "x$1" = "xstart" ]; then

	
    if [ "x$2" = "x" ]; then
		heading '######################start tunnel######################';
		
		running=1;
		tunnelDoingFolder=${INSTALLDIR}/${config_doing_dir}; 
		for file_a in ${tunnelDoingFolder}/*; do 
			if test -f $file_a; then
				temp_file=`basename $file_a`  
				info $temp_file' is running.'
				running=0;
			fi
		done
		
		if [ $running -eq 1 ]; then
			# 把停止的tunnel文件加入到运行目录
			info 'move tunnel files to run directory...'
			$INSTALLDIR/recover-tunnel-sites.sh
			info 'move tunnel files to run directory end'		
			
			info 'start ssh tunnel ...'
			# 启动ssh隧道并开启定时任务
			$INSTALLDIR/autossh.sh 
			info 'start ssh tunnel end'
			
			info 'start ssh tunnel crontab ...'
			# 添加crontab任务
			$INSTALLDIR/crontab.sh --add
			info 'start ssh tunnel crontab end'			
		else
			warn 'please run '$0' stop'
		fi
	    		
	else
		heading '######################start tunnel######################';
		info 'start ssh tunnel ...'
	    tunnelName=$2;
		ssh_store_file=$ssh_key_comment'_'$tunnelName		
		if is_confirmed_tunnelsite $ssh_store_file; then
		    store_file=$confirmed_dir/$ssh_store_file;
			comment=`cut -d = -f 2 < $store_file`;
			
			if queryTunnelDoing $ssh_store_file ; then
				info $ssh_store_file' is running.' 
				warn 'please run '$0' stop '$tunnelName 
			else
				info 'start ssh tunnel ...'
				# 启动ssh隧道并开启定时任务
				$INSTALLDIR/autossh.sh $ssh_store_file'='$comment
				info 'start ssh tunnel end'	
				
				info 'start ssh tunnel crontab ...'
				# 添加crontab任务
				$INSTALLDIR/crontab.sh --add
				info 'start ssh tunnel crontab end'
			fi
						
		else
			if is_tmp_confirmed_tunnelsite $ssh_store_file; then
				# 把停止的tunnel文件加入到运行目录
				info 'move tunnel file ['$ssh_store_file'] to run directory...'
				$INSTALLDIR/recover-tunnel-sites.sh $ssh_store_file'='$comment
				info 'move tunnel file ['$ssh_store_file'] to run directory end'
				
				info 'start ssh tunnel ...'
				# 启动ssh隧道并开启定时任务
				$INSTALLDIR/autossh.sh $ssh_store_file'='$comment
				info 'start ssh tunnel end'	
			
				info 'start ssh tunnel crontab ...'
				# 添加crontab任务
				$INSTALLDIR/crontab.sh --add
				info 'start ssh tunnel crontab end'	
			else
				warn 'Tunnel named ['$tunnelName'] does not exist'
			fi			
		fi
		info 'start ssh tunnel end'
	fi
		
elif [ "x$1" = "xstop" ]; then
	
	if [ "x$2" = "x" ]; then
		heading '######################stop tunnel######################';
				
		running=1;
		tunnelDoingFolder=${INSTALLDIR}/${config_doing_dir}; 
		for file_a in ${tunnelDoingFolder}/*; do 
			if test -f $file_a; then
				info $temp_file' is running.'
				running=0;
			fi
		done
		if [ $running -eq 0 ]; then
			info 'stop ssh tunnel ...'
			# 删除crontab任务
			info 'delete crontab job ...'
			$INSTALLDIR/crontab.sh --remove
			info 'delete crontab job end'

			# 停止ssh隧道
			info 'stop screen ...'
			$INSTALLDIR/kill-screen-all.sh
			info 'stop screen end'
			
			# 杀死autossh进程
			info 'stop autossh ...'
			$INSTALLDIR/kill-autossh.sh
			info 'stop autossh end'

			# 移动autossh文件到备份目录
			info 'move autossh files to tmp directory...'
			$INSTALLDIR/move-tunnel-sites.sh
			info 'move autossh files to tmp directory end'
			
			# 删除运行中文件
			info 'delete autossh run files...'
			$INSTALLDIR/delete-tunnel-running.sh
			info 'delete autossh run files end'	

            info 'stop ssh tunnel end'			
		else
		    warn 'please run '$0' create and '$0' start'
		fi
					
		
		
	else
		heading '######################stop tunnel######################';
		info 'stop ssh tunnel ...'
	    tunnelName=$2;
		ssh_store_file=$ssh_key_comment'_'$tunnelName		
		if is_confirmed_tunnelsite $ssh_store_file; then
		    store_file=$confirmed_dir/$ssh_store_file;
			comment=`cut -d = -f 2 < $store_file`;
			tunnel_site=$ssh_store_file'='$comment
			info 'stop screen ['$ssh_store_file ']...'
			# 启动ssh隧道
		    $INSTALLDIR/kill-screen.sh $tunnel_site
			info 'stop screen '$ssh_store_file' end'
			
			# 杀死autossh进程
			info 'stop autossh ['$tunnel_site'] ...'
			$INSTALLDIR/kill-autossh.sh $tunnel_site
			info 'stop autossh ['$tunnel_site'] end'
			
			# 移动tunnel文件到临时目录
			info 'move tunnel ['$store_file'] to tmp directory...'
			$INSTALLDIR/move-tunnel-sites.sh $tunnel_site
			info 'move tunnel ['$store_file'] to tmp directory end'
			
			# 删除运行中文件
			info 'delete autossh run files...'
			$INSTALLDIR/delete-tunnel-running.sh $INSTALLDIR/$config_doing_dir/$ssh_store_file
			info 'delete autossh run files end'
			
		else
			warn 'Tunnel named ['$tunnelName'] does not exist'
		fi
		info 'stop ssh tunnel end'
	fi
	
elif [ "x$1" = "xstatus" ]; then
    content=`screen ls`;
	echo $content;
else
    help
    exit 1
fi

exit 0

# eof


